package com.atao.view;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class ProductListLinearLayout extends LinearLayout implements
		ViewFactory, OnItemSelectedListener {
	Gallery gallery;
	TextSwitcher textSwitcher;
	List<ViewBean> views;
	List<String> productIntrs;
	private int productNum;
	MyGridViewAdapter myGridViewAdapter;
	public class ViewBean {
		private View view;
		public View getView() {
			return view;
		}
		public void setView(View view) {
			this.view = view;
		}
	}
	public ProductListLinearLayout(Context context) {
		super(context);
	}

	public ProductListLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		gallery = (Gallery) findViewById(R.id.gallery);
		gallery.setOnItemSelectedListener(this);
		textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
		textSwitcher.setFactory(this);
	}
	public void initData() {
		views = new ArrayList<ViewBean>();
		productIntrs = new ArrayList<String>();
		for (String s : getPs()) {
			productIntrs.add(s);
		}
		productNum= productIntrs.size();
		for (int i = 0; i < productNum; i++) {
			ViewBean viewBean = new ViewBean();
			View view = View.inflate(getContext(), R.layout.loading_view, null);
			viewBean.setView(view);
			views.add(viewBean);
		}
		// ImageView iv=new ImageView(getContext());
		// iv.setImageResource(resId);
		myGridViewAdapter=new MyGridViewAdapter(getContext(), views,productIntrs);
		gallery.setAdapter(myGridViewAdapter);
		new MyAsyncTask().execute();
	}
	private int[] getImageIds(){
		return new int[] { R.drawable.style_analysi,
			R.drawable.style_coodration, R.drawable.watch,
			R.drawable.ic_launcher, R.drawable.background };
	}

	private String[] getPs() {
		return new String[] { "a", "b", "c", "d", "e" };
	}

	class MyAsyncTask extends AsyncTask<Void, Void, File> {
		@Override
		protected File doInBackground(Void... params) {
			int[] images=getImageIds();
			for(int i=0;i<productNum;i++){
				ViewBean viewBean=myGridViewAdapter.getItem(i);
				try {
					Thread.sleep(1000);
				} catch (Exception e) {
					e.printStackTrace();
				}
				int maxWidth=MainActivity.width/3;
				ImageView iv=new ImageView(getContext());
				iv.setImageResource(images[i]);
				iv.setScaleType(ScaleType.FIT_XY);
				iv.setLayoutParams(new Gallery.LayoutParams(
						maxWidth,
						Gallery.LayoutParams.FILL_PARENT) );
				viewBean.setView(iv);
			}
			publishProgress();
			File file = new File("c://");
			return file;
		}

		@Override
		protected void onPostExecute(File file) {
		}
		@Override
		protected void onProgressUpdate(Void... values) {
			if (isCancelled())
				return;
			myGridViewAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public View makeView() {
		TextView tv = new TextView(getContext());
		return tv;
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		if(position < 1)
			  gallery.setSelection(1);
		String text = view.getTag().toString();
		textSwitcher.setText(text);
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
	}
}
